package com.mrjy.maintain.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mrjy.common.dto.SMSSendDto;
import com.mrjy.common.dto.UserHappinessDto;
import com.mrjy.common.entity.SMSSendInfo;
import com.mrjy.common.entity.UserBindEmail;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.entity.VideoInfo;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.common.util.ResponseCode;
import com.mrjy.maintain.bo.ISMSSendInfoBo;
import com.mrjy.maintain.bo.IUserInfoBo;
import com.mrjy.maintain.dao.IUserInfoDao;
import com.mrjy.maintain.util.RoleInfoEnum;
import com.mrjy.wechat.conts.WxConsts;

@Controller
@RequestMapping("/maintain/userInfo")
public class UserInfoController {
	@Resource
	private IUserInfoBo userInfoBo;
	@Autowired
	private IUserInfoDao userInfoDao ;
	@Resource
	private ISMSSendInfoBo smsSendInfoBo;

	/**
	 * 后台添加用户
     *
     * @param userInfo
     * @return
	 */
	@RequestMapping("/maintainAddUserInfo")
	@ResponseBody
	public Object maintainAddUserInfo(UserInfo userInfo, HttpSession session) {
		int row = userInfoBo.txmaintainAddUserInfo(userInfo);
		if (row != 1) {
			return "error";
		}
		return "ok";
	}

	/**
	 * 后台添加讲师
     *
     * @param userInfo
     * @return
	 */
	@RequestMapping("/maintainAddTeacherInfo")
	@ResponseBody
	public Object maintainAddTeacherInfo(UserInfo userInfo, HttpSession session) {
		int row = userInfoBo.txmaintainAddUserInfo(userInfo);
		if (row != 1) {
			return "error";
		}
		return "ok";
	}

	/**
	 * 快速注册前端
     *
     * @param userInfo
     * @return
	 */
	@RequestMapping("/fastAddUserInfo")
	@ResponseBody
	public Object fastAddUserInfo(UserInfo userInfo, String mobileValCode,
			Integer typeId, HttpSession session, HttpServletRequest request) {
		// HashMap<String, String> map = (HashMap<String, String>)
		// session.getAttribute(GlobalConstant.RANDOM_PhoneCODE_KEY);
		//session.invalidate(); // 每次登录清除Session缓存
		session = request.getSession();
		userInfo.setSessionId(session.getId());
		userInfo = userInfoBo.txFastAddUserInfo(userInfo, mobileValCode, typeId);
		if ("ok".equals(userInfo.getLoginMessage())) {
			session.setAttribute(GlobalConstant.USER_INFO, userInfo);
		}
		// session.removeAttribute(GlobalConstant.RANDOM_PhoneCODE_KEY);
		return userInfo.getLoginMessage();
	}

	/**
	 * 注册前端
     *
     * @param userInfo
     * @return
	 */
	@RequestMapping("/addUserInfo")
	@ResponseBody
	public Object addUserInfo(UserInfo userInfo, String mobileValCode,Integer typeId,HttpSession session) {
		userInfo.setSessionId(session.getId());
		Map<String, Object> restult = (Map<String, Object>) userInfoBo.txAddUserInfo(userInfo, mobileValCode,typeId);
		if(restult.get("err_code").equals(ResponseCode.SUCCESS.getCode())){
			session.setAttribute(GlobalConstant.USER_INFO, restult.get("userInfo"));
		}
		return restult;
	}

	/**
	 * 登录前端
     *
     * @param userInfo
     * @return
	 */
	@RequestMapping("/login")
	@ResponseBody
	public Object login(UserInfo userInfo, HttpSession session,HttpServletRequest request) {
		//session.invalidate(); // 每次登录清除Session缓存
		session = request.getSession();
		if (userInfo.getLogin_way()!=null && userInfo.getLogin_way().equals(WxConsts.PC_LOGIN_CODE)){
		    userInfo.setSessionIdPC(session.getId());
        }else {
            userInfo.setSessionId(session.getId());
        }
		userInfo = userInfoBo.login(userInfo,session);
		if("ok".equals(userInfo.getLoginMessage())){
			userInfo.setTocken(session.getId());
			session.setAttribute(GlobalConstant.USER_INFO, userInfo);
		}
		return userInfo;
	}

	/**
	 * 获取已登录用户信息 --用于个人中心获取登陆信息
     *
     * @param session
     * @return
	 * @author csshui true
	 */
	@RequestMapping("/sessionUserInfo")
	@ResponseBody
	public Object sessionUserInfo(HttpSession session,Integer userSessionId,HttpServletRequest request) {
		UserInfo userInfo = (UserInfo) session
				.getAttribute(GlobalConstant.USER_INFO);
		if (userInfo == null) {//微信新版悬浮窗优化
			UserInfo ui= null;
			if(userSessionId != null){
				session = request.getSession();
				ui = userInfoDao.queryById(userSessionId);
				ui.setSessionId(session.getId());
		        int update = userInfoDao.updateSession2(ui);
		        if(update == 1){
		        	session.setAttribute(GlobalConstant.USER_INFO, ui);
		        }
			}
			if(ui != null){
				return ui;
			}else{
				return "error";
			}
		} else if (!session.getId().equals(
				userInfoBo.querySessionByUserId(userInfo.getId()))
				&& !session.getId().equals(
						userInfoBo.queryPCSessionByUserId(userInfo.getId()))) {
			//session.invalidate();
			return "repeat";
		} else {
			return userInfo;
		}
	}

	/**
	 * TODO 修改更新方法，没有做事务处理 pc端用户信息更新
     *
     * @param userInfo
     *            userInfo 表字段
	 * @param session
	 * @return
	 * @author csshui true
	 */
	@RequestMapping("/updateById2")
	@ResponseBody
	public Object updateById2(UserInfo userInfo, HttpSession session) {
		UserInfo sessionUserInfo = (UserInfo) session
				.getAttribute(GlobalConstant.USER_INFO);
		if (sessionUserInfo != null) {
			sessionUserInfo = userInfoBo.updateById(userInfo, sessionUserInfo);
			session.setAttribute(GlobalConstant.USER_INFO, sessionUserInfo);
			return sessionUserInfo;
		} else {
			return "error";
		}
	}

	/**
	 * 获取当前用户(老师)名下的推荐会员
     *
     * @param session
     * @return
	 */
	@RequestMapping("/memberList")
	@ResponseBody
	public Object memberList(HttpSession session, UserInfo userInfo) {
		UserInfo sessionUserInfo = (UserInfo) session
				.getAttribute(GlobalConstant.USER_INFO);
		return userInfoBo.getMemberList(sessionUserInfo, userInfo);
	}

	@RequestMapping("/teacherListByLabel")
	@ResponseBody
	public Object teacherListByLabel(HttpSession session, Integer labelId) {
		UserInfo sessionUserInfo = (UserInfo) session
				.getAttribute(GlobalConstant.USER_INFO);
		return userInfoBo.userList(sessionUserInfo, labelId);
	}

	/**
	 * 获取老师名下的发展会员----后台
     *
     * @param session
     * @param userInfo
	 * @return
	 */
	@RequestMapping("/getMemberListByPageAdmin")
	@ResponseBody
	public Object getMemberListByPageAdmin(HttpSession session,
			UserInfo userInfo) {
		UserInfo sessionUserInfo = (UserInfo) session
				.getAttribute(GlobalConstant.USER_INFO);
		return userInfoBo.getMemberListAdmin(sessionUserInfo, userInfo);
	}

	/**
	 * 获取老师名下的发展会员总数和提成金额
     *
     * @param session
     * @param userInfo
	 * @return
	 */
	@RequestMapping("/getMemberListTotalAdmin")
	@ResponseBody
	public Object getMemberListTotalAdmin(HttpSession session, UserInfo userInfo) {
		UserInfo sessionUserInfo = (UserInfo) session
				.getAttribute(GlobalConstant.USER_INFO);
		return userInfoBo.getMemberTotalAdmin(sessionUserInfo, userInfo);
	}

	/**
	 * 导出老师的发展会员列表会员信息的Excel表格
     *
     * @param session
     * @param response
	 * @param paraInfo
	 * @return
	 */
	@RequestMapping("/exportMemberListByTeacher")
	@ResponseBody
	public Object exportMemberListByTeacher(HttpSession session,
			HttpServletResponse response, UserInfo paraInfo) {
		UserInfo sessionUserInfo = (UserInfo) session
				.getAttribute(GlobalConstant.USER_INFO);
		return userInfoBo.exportMemberList(response, sessionUserInfo, paraInfo);
	}

	/**
	 * 手机验证是否已用
     *
     * @param mobilePhone
     * @param mobilePhoneSrc
	 * @return
	 */
	@RequestMapping("/mobilePhoneValidate")
	@ResponseBody
	public Object mobilePhoneValidate(String mobilePhone, String stid) {
		if (StringUtils.isBlank(stid)) {
			stid = null;
		}
		return userInfoBo.mobilePhoneValidate(mobilePhone, stid);
	}

	/**
	 * 服务站管理员手机验证
     *
     * @param adminMobilePhone
     * @return
	 */
	@RequestMapping("/adminMobliePhoneValidate")
	@ResponseBody
	public Object adminMobliePhoneValidate(String adminMobilePhone, String stid) {
		return userInfoBo.mobilePhoneValidate(adminMobilePhone, stid);
	}

	/**
	 * 服务站添加管理员账号验证
     *
     * @return
     */
	@RequestMapping("/adminLoginCodeValidate")
	@ResponseBody
	public Object adminLoginCodeValidate(String adminLoginCode, String stid) {
		return userInfoBo.loginCodeValidate(adminLoginCode, stid);
	}

	/**
	 * email 验证可用
     *
     * @param email
     * @return
	 */
	@RequestMapping("/emailValidate")
	@ResponseBody
	public String emailValidate(String email, String emailSrc) {
		return userInfoBo.emailValidate(email, emailSrc);
	}

	/**
	 * 修改用户手机号码
     *
     * @param mobilePhone
     * @param session
	 * @return 废弃
	 */
	@RequestMapping("/updateMobilePhone")
	@ResponseBody
	public Object updateMobilePhone(String mobilePhone, HttpSession session) {
		UserInfo sessionUserInfo = (UserInfo) session
				.getAttribute(GlobalConstant.USER_INFO);
		return userInfoBo.updateMobilePhone(mobilePhone, sessionUserInfo);
	}

	/**
	 * 邮箱绑定 个人中心 -- 账户安全 -- 邮箱绑定
     *
     * @param userBindEmail
     *            .bindingEmail 邮箱
	 * @param session
	 * @return
	 */
	@RequestMapping("/emailBind")
	@ResponseBody
	public Object emailBind(UserBindEmail userBindEmail, HttpSession session,
			HttpServletRequest request) {
		UserInfo sessionUserInfo = (UserInfo) session
				.getAttribute(GlobalConstant.USER_INFO);
		userBindEmail.setIp(request.getRemoteAddr());
		return userInfoBo.emailBind(userBindEmail, sessionUserInfo);
	}

	/**
	 * TODO sendSMS方法有insert操作，没有做事务处理 验证手机号码并发送验证码 web----验证未绑定手机 个人中心 -- 账户安全
	 * --手机绑定--发送验证码
     *
     * @param mobilePhone
     *            手机号码
	 * @param session
	 * @author csshui isUser=true
	 * @return
	 */
	@RequestMapping("/sendMobilePhoneCode")
	@ResponseBody
	public Object sendMobilePhoneCode(String mobilePhone, HttpSession session) {
		UserInfo sessionUserInfo = (UserInfo) session
				.getAttribute(GlobalConstant.USER_INFO);
		if (sessionUserInfo == null) {
			return "error";
		}
		String flag = (String) userInfoBo.sendMobilePhoneCode(mobilePhone);
		if (flag == "ok") {
			SMSSendDto smsSendDto = new SMSSendDto();
			smsSendDto.setRecNum(mobilePhone);
			smsSendDto.setProduct("365明人课堂");
			smsSendDto.setTypeId(1);
			int result = smsSendInfoBo.sendSMS(smsSendDto);
			if (result == 0) {
				return "sendError";
			} else {
				return flag;
			}
		} else {
			return flag;
		}
	}

	/**
	 * 验证手机验证码并绑定 web 个人中心 --账号管理 -- 绑定手机号
     *
     * @param mobilePhone
     *            新手机号码
	 * @param mobilePhoneCode
	 *            验证码
	 * @param request
	 * @param session
	 * @author csshui isUse = true
	 * @return
	 */
	@RequestMapping("/mobilePhoneBind")
	@ResponseBody
	public Object mobilePhoneBind(String mobilePhone, String mobilePhoneCode,
			HttpServletRequest request, HttpSession session) {
		UserInfo sessionUserInfo = (UserInfo) session
				.getAttribute(GlobalConstant.USER_INFO);
		if (sessionUserInfo == null) {
			return "userError";
		}
		SMSSendInfo smsSendInfo = new SMSSendInfo();
		smsSendInfo.setRecNum(mobilePhone);
		smsSendInfo.setSendCode(mobilePhoneCode);
		smsSendInfo.setSmsCode("SMS_16440022");
		smsSendInfo = smsSendInfoBo.validateSMSCode(smsSendInfo);
		if (smsSendInfo.getSmsMessage().equals("ok")) {
			UserInfo userInfo = new UserInfo();
			userInfo.setMobilePhone(mobilePhone);
			userInfo.setId(sessionUserInfo.getId());
			String status = userInfoBo.txPhoneBind(userInfo, mobilePhoneCode);
			// 绑定成功后，把手机号更新到session缓存中
			if ("ok".equals(status)) {
				sessionUserInfo.setMobilePhone(mobilePhone);
				session.setAttribute(GlobalConstant.USER_INFO, sessionUserInfo);
			}
			return status;
		}
		return smsSendInfo.getSmsMessage();
	}

	/**
	 * TODO sendSMS有insert操作，没有做事务处理 发送验证码 ----支付密码修改
     *
     * @param mobilePhone
     *            手机号码
	 * @param session
	 * @author csshui isUse=true
	 * @return
	 */
	@RequestMapping("/sendMobilePhoneCodePayPwed")
	@ResponseBody
	public Object sendMobilePhoneCodePayPwed(HttpSession session) {
		UserInfo sessionUserInfo = (UserInfo) session
				.getAttribute(GlobalConstant.USER_INFO);
		// Object object =
		// userInfoBo.sendMobilePhoneCodeLogined(sessionUserInfo);
		if (sessionUserInfo == null) {
			return "userInfo_error";
		} else {
			SMSSendDto smsSendDto = new SMSSendDto();
			smsSendDto.setRecNum(sessionUserInfo.getMobilePhone());
			smsSendDto.setProduct("365明人课堂");
			smsSendDto.setTypeId(1);
			int result = smsSendInfoBo.sendSMS(smsSendDto);
			if (result == 0) {
				return "sendError";
			} else {
				return "ok";
			}
		}
	}

	/**
	 * 推荐码验证
     *
     * @param recommendCode
     * @return
	 */
	@RequestMapping("/recommendCodeValidate")
	@ResponseBody
	public Object recommendCodeValidate(String recommendCode,String recommendCodeSrc) {
		return userInfoBo.recommendCodeValidate(recommendCode, recommendCodeSrc);
	}

	/**
	 * 获取随机推荐码
     *
     * @return
     */
	@RequestMapping("/getRecommendCode")
	@ResponseBody
	public String getRecommendCode() {
		return userInfoBo.getRecommendCode();
	}

	/**
	 * 验证原密码HTML5 个人中心 -- 账号安全--修改密码 使用
     *
     * @param passwordSrc
     *            旧密码
	 * @param session
	 * @author csshui isUser = true
	 * @return
	 */
	@RequestMapping("/passwordValidate")
	@ResponseBody
	public Object passwordValidate(String passwordSrc, HttpSession session) {
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if (userInfo != null) {
			return userInfoBo.passwordValidate(userInfo, passwordSrc);
		} else {
			return "error";
		}
	}

	/**
	 * TODO 更新操作没有做事务处理 用户修改密码html 个人中心 -- 账号安全--修改密码 使用
     *
     * @param passwordSrc
     *            旧密码
	 * @param passwordNew
	 *            新密码
	 * @param session
	 * @author csshui isUser=true
	 * @return
	 */
	@RequestMapping("/updatePassword")
	@ResponseBody
	public Object updatePassword(String passwordSrc, String passwordNew,
			HttpSession session) {
		UserInfo userInfo = (UserInfo) session
				.getAttribute(GlobalConstant.USER_INFO);
		return userInfoBo.txChangePassword(userInfo, passwordSrc, passwordNew);
	}

	/**
	 * 后台管理员重置密码
     *
     * @param passwordNew
     * @param session
	 * @return
	 */
	@RequestMapping("/resetPassword")
	@ResponseBody
	public Object resetPassword(UserInfo user, HttpSession session) {
		return userInfoBo.resetPassword(user);
	}

	/**
	 * 根据角色加载用户后台
     *
     * @param userInfo
     * @return
	 */
	@RequestMapping("/userList")
	@ResponseBody
	public Object userList(UserInfo userInfo, HttpSession session) {
		return userInfoBo.userList(userInfo);
	}

	/**
	 * 根据角色加载用户后台
     *
     * @param userInfo
     * @return
	 */
	@RequestMapping("/userListNoPage")
	@ResponseBody
	public Object userListNoPage(UserInfo userInfo, HttpSession session) {
		return userInfoBo.userListNoPage(userInfo);
	}

	/**
	 * 账号验证前端
     *
     * @return
     */
	@RequestMapping("/loginCodeValidate")
	@ResponseBody
	public Object loginCodeValidate(String loginCode) {
		return userInfoBo.loginCodeValidate(loginCode, null);
	}

	/**
	 * 根据ID查询用户
     *
     * @return
     */
	@RequestMapping("/queryById")
	@ResponseBody
	public Object queryById(int id) {
		UserInfo user = userInfoBo.queryById(id);
		return user;
	}

	/**
	 * 修改
     *
     * @param userInfo
     * @return
	 */
	@RequestMapping("/updateById")
	@ResponseBody
	public Object updateById(HttpSession session, UserInfo userInfo)
			throws UnsupportedEncodingException {
		int count = userInfoBo.txUpdateById(userInfo);
		if (count > 0) {
			userInfo = userInfoBo.queryById(userInfo.getId());
			session.setAttribute(GlobalConstant.USER_INFO, userInfo);
			return "ok";
		}
		return "error";
	}

	/**
	 * 删除
     *
     * @param ids
     * @return
	 */
	@RequestMapping("/deleteByIds")
	@ResponseBody
	public Object deleteByIds(int[] ids) {
		return userInfoBo.txDeleteByIds(ids);
	}

	/**
	 * 禁用、起用
     *
     * @param ids
     * @return
	 */
	@RequestMapping("/updateDisableFlag")
	@ResponseBody
	public Object updateDisableFlag(UserInfo userInfo,HttpSession session) {
		return userInfoBo.updateDisableFlag(userInfo,session);
	}

	/**
	 * 查询教师标签列表
     *
     * @return
     */
	@RequestMapping("/lableTypeList")
	@ResponseBody
	public Object lableTypeList() {
		return userInfoBo.queryLableTypeList();
	}

	/**
	 * 微信openid登录
     *
     * @param opid
     * @param session
	 * @return
	 * @author Javin : isUer = true
	 */
	@RequestMapping("/weixinOpenidLogin")
	@ResponseBody
	public Object weixinOpenidLogin(UserInfo info, HttpSession session,
			HttpServletRequest request) {
		//session.invalidate(); // 每次登录清除Session缓存
		session = request.getSession();
		// 设置opid到服务器
		UserInfo userInfo = userInfoBo.weixinLogin(info, session.getId());
		session.setAttribute(GlobalConstant.USER_INFO, userInfo);
        return userInfo;
    }

	@RequestMapping("/sessionWeixinOpenid")
	@ResponseBody
	public Object sessionWeixinOpenid(HttpSession session,HttpServletRequest request) {
		if (session.getAttribute("weixin_openid") != null) {
			return session.getAttribute("weixin_openid");
		}
		return "";
	}

	@RequestMapping("/queryTeacherPage")
	@ResponseBody
	public Object queryTeacherPage(UserInfo userInfo) {
		userInfo.setRoleId(RoleInfoEnum.TEACHER.getRoleId());
		return userInfoBo.queryPageByRoleId(userInfo);
	}

	/**
     *
     * @param mobilePhone
     * @return error:不存在该手机， userInfo:存在并返回对象值
	 * @author Javin : isUer = true
	 */
	@RequestMapping("/queryByMobilePhone")
	@ResponseBody
	public Object queryByMobilePhone(String mobilePhone) {
		return userInfoBo.queryByMobilePhone(mobilePhone);
	}

	/**
	 * 通过手机号码查询是不已被注册
     *
     * @author csshui
     * @param mobilePhone
	 * @return
	 */
	@RequestMapping("/mobilePhoneValid")
	@ResponseBody
	public Object mobilePhoneValidate(@RequestParam("mobilePhone") String mobilePhone) {
		return userInfoBo.mobilePhoneValid(mobilePhone);
	}

	/**
	 * 推荐码验证
     *
     * @param shareCode
     * @author csshui
	 * @return
	 */
	@RequestMapping("/validShareCode")
	@ResponseBody
	public Object validShareCode(@RequestParam("shareCode") String shareCode) {
		return userInfoBo.validShareCode(shareCode);
	}

	/**
	 * @param openId
	 * @param session
	 * @param request
	 * @author Javin : isUer = true
	 */
	@RequestMapping("/autoLogin")
	@ResponseBody
	public Object autoLogin(@RequestParam("openId") String openId,HttpSession session, HttpServletRequest request) {
		// HashMap<String, String> map = (HashMap<String, String>)
		// session.getAttribute(GlobalConstant.RANDOM_PhoneCODE_KEY);
		//session.invalidate(); // 每次登录清除Session缓存
		session = request.getSession();
		UserInfo userInfo = userInfoBo.txAutoLogin(openId, session.getId());
		if ("success".equals(userInfo.getLoginMessage())) {
			session.setAttribute(GlobalConstant.USER_INFO, userInfo);
		}
		// session.removeAttribute(GlobalConstant.RANDOM_PhoneCODE_KEY);
		return userInfo.getLoginMessage();
	}

	/**
	 * 根据openId 查询信息，use：校验手机号跟openId有没有匹配
     *
     * @param info
     *            .openId
	 * @return
	 * @author Javin :isUse = true;
	 */
	@RequestMapping("/queryByOpenId")
	@ResponseBody
	public Object queryByOpenId(UserInfo info) {
		return userInfoBo.queryByOpenId(info);
	}

	/**
	 * 导师推荐查询方法，根据视频数倒序
     *
     * @param info , sortType 排序（new ,hot）
     * @return
	 * @author Javin : isUer = true, isNew = true
	 */
	@RequestMapping("/queryPageTeacherByVideoDesc")
	@ResponseBody
	public Object queryPageTeacherByVideoDesc(UserInfo info) {
		return userInfoBo.queryPageTeacherByVideoDesc(info);
	}

	@RequestMapping("/queryPageTeacherBySortType")
	@ResponseBody
	public Object queryPageTeacherBySortType(VideoInfo info) {
		return userInfoBo.queryPageTeacherBySortType(info);
	}

	/**
	 * 导师查询方法，根据视频类标签
     *
     * @param info
     *            ,videoInfo
	 * @return
	 * @author lyon
	 */
	@RequestMapping("/queryPageTeacherByTypename")
	@ResponseBody
	public Object queryPageTeacherByTypename(VideoInfo videoInfo) {
		return userInfoBo.queryPageTeacherByTypename(videoInfo);
	}

	/**
	 * 校验微信绑定手机号：status: 已被绑定，老用户(会员)
     *
     * @param mobilePhone
     * @param openId
	 * @author Javin isUer = true; isNew = true;
	 */
	@RequestMapping("/validateMobilePhone")
	@ResponseBody
	public Object validateMobilePhone(@RequestParam("mobilePhone") String mobilePhone,@RequestParam("openId") String openId) {
		return userInfoBo.validateMobilePhone(mobilePhone, openId);
	}

	/**
	 * 微信绑定手机号
     *
     * @param info
     * @param session
	 * @author Javin :isUse = true, isNew = true
	 */
	@RequestMapping("/toBinding")
	@ResponseBody
	public Object toBinding(UserInfo info, HttpSession session,HttpServletRequest request) {
		@SuppressWarnings("unchecked")
		Map<String, Object> map = (Map<String, Object>) userInfoBo.txToBinding(info);
		if (map != null && (int) map.get("err_code") == 0) {// 绑定成功，用openId查询最新记录并缓存在session
			//session.invalidate();
			session = request.getSession();
			UserInfo userInfo = userInfoBo.queryByWeixinOpenid(info.getWeixinOpenid());
			if (null != userInfo) {
				userInfo.setSessionId(session.getId());
				userInfoBo.updateById(userInfo);
				session.setAttribute(GlobalConstant.USER_INFO, userInfo);
			}
		}
		return map;

	}

	/**
	 * 通过微信openId查询是否为会员
     *
     * @param openId
     * @return
	 */
	@RequestMapping("/isVipByOpenId")
	@ResponseBody
	public Object isVipByOpenId(String openId) {
		return userInfoBo.isVipByOpenId(openId);

	}

	/**
	 * 通过id查询老师的相关数据
     *
     * @param openId
     * @return
	 */
	@RequestMapping("/getTeacherInfoById")
	@ResponseBody
	public Object getTeacherInfoById(int id) {
		return userInfoBo.getTeacherInfoById(id);
	}

	/**
	 * 根据老师名字模糊分页收索
     *
     * @param info
     * @return
	 * @author Javin : isUer =true, isNew = true
	 */
	@RequestMapping("/getPageTeacherByName")
	@ResponseBody
	public Object getPageTeacherByName(UserInfo info) {
		return userInfoBo.getPageTeacherByName(info);
	}

	/**
	 * 根据openId查询是否有绑定手机号码
     *
     * @param openId
     * @return
	 */
	@RequestMapping("/isBindedByOpenId")
	@ResponseBody
	public Object isBindedByOpenId(@RequestParam("openId") String openId) {
		return userInfoBo.isBindedByOpenId(openId);
	}

	/**
	 * 根据openId查询是否有绑定手机号码
     *
     * @param openId
     * @return
	 */
	@RequestMapping("/weChatToBindMobilePhone")
	@ResponseBody
	public Object weChatToBindMobilePhone(
			@RequestParam("openId") String openId,
			@RequestParam("mobilePhone") String mobilePhone,
			@RequestParam("mobilePhoneCode") String mobilePhoneCode,
			@RequestParam("typeId") Integer typeId, HttpSession session,
			HttpServletRequest request) {
		@SuppressWarnings("unchecked")
		Map<String, Object> map = (Map<String, Object>) userInfoBo.weChatToBindMobilePhone(openId, mobilePhone, mobilePhoneCode,typeId);
		if (map != null && (int) map.get("err_code") == 0) {// 绑定成功，用openId查询最新记录并缓存在session
			//session.invalidate();
			session = request.getSession();
			UserInfo userInfo = userInfoBo.queryByWeixinOpenid(openId);
			if (null != userInfo) {
				userInfo.setSessionId(session.getId());
				userInfoBo.updateById(userInfo);
				session.setAttribute(GlobalConstant.USER_INFO, userInfo);
			}
		}
		return map;
	}
	/**
	 * 根据UnionId查询是否有绑定手机号码
     * @param openId
     * @return
	 */
	@RequestMapping("/bingPhoneByUnionId")
	@ResponseBody
    public Object bingPhoneByUnionId(
    		@RequestParam("mobilePhone") String mobilePhone,
			@RequestParam("mobilePhoneCode") String mobilePhoneCode,
			@RequestParam("typeId") Integer typeId, HttpSession session){
		return userInfoBo.bingPhoneByUnionId(mobilePhone, mobilePhoneCode,typeId,session);
    }

	/**
	 * 根据id查询是否有绑定手机号码
     *
     * @param id
     * @return
	 */
	@RequestMapping("/idToBindMobilePhone")
	@ResponseBody
	public Object idToBindMobilePhone(@RequestParam("id") String id,
			@RequestParam("mobilePhone") String mobilePhone,
			@RequestParam("mobilePhoneCode") String mobilePhoneCode,
			HttpSession session, HttpServletRequest request, int typeId) {
		@SuppressWarnings("unchecked")
		Map<String, Object> map = (Map<String, Object>) userInfoBo.commomToBindMobilePhone(id, mobilePhone, mobilePhoneCode,typeId);
		if (map != null && (int) map.get("err_code") == 0) {// 绑定成功，用id查询最新记录并缓存在session
			//session.invalidate();
			session = request.getSession();
			UserInfo userInfo = userInfoBo.queryById(Integer.parseInt(id));
			if (null != userInfo) {
				userInfo.setSessionId(session.getId());
				userInfoBo.updateById(userInfo);
				session.setAttribute(GlobalConstant.USER_INFO, userInfo);
			}
		}
		return map;
	}

	/**
	 * 导师推荐查询方法，运营部自定义对外排序(orderNum)
     *
     * @param info
     * @return
	 * @author Javin : isUer = true, isNew = true TODO 需要添加后端排序字段orderNum
	 */
	@RequestMapping("/queryPageTeacherByOrderNum")
	@ResponseBody
	public Object queryPageTeacherByOrderNum(UserInfo info) {
		return userInfoBo.queryPageTeacherByOrderNum(info);
	}

	/**
	 * 用户做推广时为当前用户生成推广码
     *
     * @param info
     *            .userId
	 * @return
	 * @author Javin : isUer = true, isNew = true
	 */
	@RequestMapping("/insertShareCodeByUserId")
	@ResponseBody
	public Object insertShareCodeByUserId(UserInfo info) {
		return userInfoBo.txInsertShareCodeByUserId(info);
	}

	/**
     *
     * @param info
     * @return
	 */
	@RequestMapping("/loginForPC")
	@ResponseBody
	public Object loginForPC(UserInfo userInfo, HttpSession session,HttpServletRequest request) {
		return login(userInfo, session, request);
	}

	/**
     *
     * @param info
     * @return
	 */
	@RequestMapping("/registerForPC")
	@ResponseBody
	public Object registerForPC(UserInfo info, String mobileValCode, int typeId) {
		return userInfoBo.registerForPC(info, mobileValCode, typeId);
	}

	/**
	 *
	 * @param info
	 * @return
	 */
	@RequestMapping("/loginoutForPC")
	@ResponseBody
	public Object loginoutForPC(HttpSession session) {
		session.removeAttribute(GlobalConstant.USER_INFO);
		return "login out";
	}

	/**
	 * 根据分享码判断是否为合作方的分享码或者合作方的推广人
     *
     * @param recommendCode
     * @return
	 */
	@RequestMapping("/isPartnerByRecommendCode")
	@ResponseBody
	public Object isPartnerByRecommendCode(String recommendCode) {
		return userInfoBo.isPartnerByRecommendCode(recommendCode);
	}

	/**
	 * 根据分享码判断是否导师的二维码
     *
     * @param recommendCode
     * @return
	 */
	@RequestMapping("/isTeacherByScanShareCode")
	@ResponseBody
	public Object isTeacherByScanShareCode(String recommendCode) {
		return userInfoBo.isTeacherByScanShareCode(recommendCode);
	}

	/**
	 * 根据用户名称检查用户是否存在
     *
     * @param userName
     *            用户名称
	 * @return
	 */
	@RequestMapping("/isExistSelectByUserName")
	@ResponseBody
	public Object isExistSelectByUserName(String userName) {
		return userInfoBo.isExistSelectByUserName(userName);
	}

	/**
	 * 拥有语音权限的导师
     *
     * @return
     */
	@RequestMapping("/queryPageTeacherWithRecord")
	@ResponseBody
	public Object queryPageTeacherWithRecord(UserInfo info) {
		return userInfoBo.queryPageTeacherWithRecord(info);
	}

	/**
	 * 更新导师价格
	 *
	 * @param userInfo
	 *            userInfo 表字段
	 * @return
	 */
	@RequestMapping("/updatePriceById")
	@ResponseBody
	public Object updatePriceById(UserInfo userInfo) {
		return userInfoBo.updatePriceById(userInfo);
	}

	/**
	 * 分页查询用户(开放平台--用户)
	 * @param userInfo
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@RequestMapping("/queryPageUser")
	@ResponseBody
	public Object queryPageUser(UserInfo userInfo,HttpSession session) {
		return userInfoBo.queryPageUser(userInfo,session);
	}


    /**
     * 导出excel
     *
     * @param accessLog
     * @return
     * @throws Exception
     */
    @RequestMapping("/export")
	@ResponseBody
	public Object export(HttpServletResponse response,UserInfo userInfo,HttpSession session){
		return userInfoBo.exportExl(userInfo,response,session);
	}


    /**
     * 添加管理员
	 * @return
	 */
	@RequestMapping(value="/addAdminUser", method=RequestMethod.POST)
	@ResponseBody
	public Object addAdminUser(UserInfo userInfo,HttpSession session){
		return userInfoBo.addAdminUser(userInfo,session);
	}

	/**
	 * 分页查询管理员
	 * @return
	 */
	@RequestMapping("/queryAdminUserByPage")
	@ResponseBody
	public Object queryAdminUserByPage(UserInfo userInfo,HttpSession session){
		return userInfoBo.queryAdminUserByPage(userInfo,session);
	}

	/**
	 * 修改管理员
	 * @return
	 */
	@RequestMapping("/updateAdminUserById")
	@ResponseBody
	public Object updateAdminUserById(UserInfo userInfo,HttpSession session){
		return userInfoBo.updateAdminUserById(userInfo,session);
	}


    /**
     * @author Javin
     * @date 2017年9月14日
     * @desc 开放平台 登录
     * @param userInfo
     * @return
     */
    @RequestMapping("/loginAdmin")
    @ResponseBody
    public Object loginAdmin(UserInfo userInfo,HttpSession session){
    	//session.invalidate(); //每次登录清除Session缓存
		userInfo.setSessionId(session.getId());
		//用于区分微信前端登录和开放平台登录
		session.setAttribute("jsessionId", session.getId());
    	return userInfoBo.loginAdmin(userInfo,session);
    }

    /**
     * 开放平台 退出登录
     * @param session
     * @return
     */
    ConcurrentMap<String, Object> map = new ConcurrentHashMap<String, Object>();

    @RequestMapping("/logoutAdmin")
    @ResponseBody
    public Object logoutAdmin(final HttpSession session){
    	new Thread(){
            public void run(){
            	try {
                  Thread.sleep(1500);
                  int flag = (int) map.get("flag");
          		if (flag==0) {
          			session.invalidate();
          		}else {
          			flag = 0;
          			map.put("flag", flag);
          		}
               } catch (InterruptedException e) { }
            }
         }.start();
    	return "ok";
    }

    /**
     * 用于判断前端是刷新还是关闭浏览器
     */

    @RequestMapping("/checkCloseOrRefresh")
	@ResponseBody
	public Object checkCloseOrRefresh(int flag) {
		if (flag == 1) { //前端传来1,则是刷新,不清除session
			this.map.put("flag", flag);
		}
		return "ok";
	}

    /**
     * 用于前端获取sessionId,避免跨域请求(停用)
     * @param session
     * @return
     */
    @RequestMapping("/getSessionId")
	@ResponseBody
    public Object getSessionId(HttpSession session){
    	Map<String, Object> map = new HashMap<String, Object>();
    	map.put("sessionId", session.getId());
    	return map;
    }

    /**
     * 拥有语音权限的导师
     *
     * @return
     */
    @RequestMapping("/queryTeacherInfoWithRecord")
    @ResponseBody
    public Object queryTeacherInfoWithRecord(UserInfo info) {
        return userInfoBo.queryTeacherInfoWithRecord(info);
    }

    /**
     * 银行卡接口
     *
     * @return
     */
    @RequestMapping("/updateBankcardByInfo")
    @ResponseBody
    public Object updateBankcardByInfo(UserInfo userInfo, HttpSession session) {
        return userInfoBo.updateBankcardByInfo(userInfo, session);
    }

    /**
     * 银行卡接口 查询
     */
    @RequestMapping("/queryBankCardInfoById")
    @ResponseBody
    public Object queryBankCardInfoById(UserInfo info) {
        return userInfoBo.queryBankCardInfoById(info);
    }
    /**
     * hfei 查询用户在点赞总榜的排名
     * @return
     */
    @RequestMapping("/queryMyPraise")
    @ResponseBody
    public Object queryMyPraise(UserInfo info) {
        return userInfoBo.queryMyPraise(info);
    }

    /**
     * 微信1.6 幸福的爱
     * 查询爱值排行榜  传date代表查今天的排行榜      不传则查历史总榜
     * @return
     */
    @RequestMapping("/queryMyHappiness")
    @ResponseBody
    public Object queryMyHappiness(UserHappinessDto dto){
    	return userInfoBo.queryMyHappiness(dto);
    }

    /**
     * 微信1.6 幸福的爱
     * 查询幸福的爱折线图  传date
     * @return
     */
    @RequestMapping("/queryMyHappinessChart")
    @ResponseBody
    public Object queryMyHappinessChart(UserHappinessDto dto){
    	return userInfoBo.queryMyHappinessChart(dto);
    }

    /**
     * @author Javin
     * @date 2018年1月24日
     * @desc 登录
     * @param userInfo
     * @param session
     * @return
     */
    @RequestMapping("/loginWeb")
    @ResponseBody
    public Object loginWeb(UserInfo userInfo, HttpSession session){
    	userInfo.setSessionId(session.getId());
    	Map<String, Object> result = userInfoBo.loginWeb(userInfo);
    	if(result.get("err_code").equals(ResponseCode.SUCCESS.getCode())){
    		session.setAttribute(GlobalConstant.USER_INFO, result.get("userInfo"));
    	}
    	return result;
    }

    /**
     * 明人 1.3.1 学习记录
     *
     * @return
     */
    @RequestMapping("/queryStudyRecord")
    @ResponseBody
    public Object queryStudyRecord(UserHappinessDto dto, HttpSession session) {
        return userInfoBo.queryStudyRecord(dto, session);
    }

    /**
     * 明人 1.3.1 学习记录
     *
     * @return
     */
    @RequestMapping("/exportStudyRecord")
    @ResponseBody
    public Object exportStudyRecord(UserHappinessDto info, HttpServletResponse response, HttpSession session) {
        return userInfoBo.exportStudyRecord(info, response, session);
    }

    /**
     * 微信1.6.4 加载[成长]页面数据 传userId, 游客为0
     * @param info
     * @return
     */
    @RequestMapping("/queryInfoByUserId")
    @ResponseBody
    public Object queryInfoByUserId(UserInfo info,HttpSession session){
    	return userInfoBo.queryInfoByUserId(info,session);
    }

    /**
     * 根据userId查询用户当前剩余积分
     * @param userId
     * @return
     */
    @RequestMapping("/queryIntegralBalanceByUserId")
    @ResponseBody
    public Object queryIntegralBalanceByUserId(Integer userId){
    	return userInfoBo.queryIntegralBalanceByUserId(userId);
    }

    /**
     * 用户管理--变更手机号码
     * @param userInfo.id userInfo.mobilePhone
     * @return
     */
    @RequestMapping("/editMobilePhoneById")
    @ResponseBody
    public Object editMobilePhoneById(UserInfo info ){
    	return userInfoBo.editMobilePhoneById(info);
    }

    /**
     * 查询智慧用户
     *
     * @param userInfo.id userInfo.mobilePhone
     * @return
     */
    @RequestMapping("/queryWisdomUser")
    @ResponseBody
    public Object queryWisdomUser(UserInfo info) {
        return userInfoBo.queryWisdomUser(info);
    }

    /**
     * 查询幸福用户
     *
     * @param userInfo.id userInfo.mobilePhone
     * @return
     */
    @RequestMapping("/queryHappinessRank")
    @ResponseBody
    public Object queryHappinessRank(UserHappinessDto info) {
        return userInfoBo.queryHappinessRank(info);
    }

    /**
     * 查询坚持型
     *
     * @param userInfo.id userInfo.mobilePhone
     * @return
     */
    @RequestMapping("/queryKeepDayRankingList")
    @ResponseBody
    public Object queryKeepDayRankingList(UserInfo info) {
        return userInfoBo.queryKeepDayRankingList(info);
    }

    /**
     * 查询学习型
     *
     * @param userInfo.id userInfo.mobilePhone
     * @return
     */
    @RequestMapping("/queryStudyUser")
    @ResponseBody
    public Object queryStudyUser(UserInfo info) {
        return userInfoBo.queryStudyUser(info);
    }


    /**
     * 查询幸福墙
     *
     * @param userInfo.id userInfo.mobilePhone
     * @return
     */
    @RequestMapping("/queryHappinessWall")
    @ResponseBody
    public Object queryHappinessWall(UserInfo info) {
        return userInfoBo.queryHappinessWall(info);
    }
    
    /**
     * 查询用户已报活动
     * 可分页
     * @param session
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/activity/list")
    @ResponseBody
    public Object list(HttpSession session,
    		@RequestParam Integer page,
    		@RequestParam Integer rows){
    	return userInfoBo.queryPaidActivityByUserId(session,page,rows);
    }
    
    /**
     * 微信头像下载到本地
     *
     * @param userInfo.id userInfo.mobilePhone
     * @return
     */
    @RequestMapping("/headimg/local")
    @ResponseBody
    public Object downLoadHeadImg(HttpSession session) {
    	Map<String , Object> map = new HashMap<String, Object>() ;
    	UserInfo info = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
    	if(info != null){
    		if(StringUtils.isBlank(info.getHeadImgUrl())){
    			map.put("err_code", "100");
    			map.put("err_msg", "用户");
    			return map ;
    		}
    	}else{
    		map.put("err_code", "-1");
    		map.put("err_msg", "用户未登录或登录失效");
    		return map ;
    	}
		
        return userInfoBo.downLoadHeadImg(info, map);
    }

}
